import { computed } from 'vue'
import { useRoute } from 'vue-router'
import { getAllPageConfigs, getPageConfigByPath } from '@/utils/page-config'
import type { PageConfig, PageConfigWithPath } from '@/types/page-config'

/**
 * 使用页面配置的组合式函数
 */
export function usePageConfig() {
  const route = useRoute()

  // 当前页面配置
  const currentPageConfig = computed<PageConfig | undefined>(() => {
    return route.meta?.pageConfig
  })

  // 获取所有页面配置
  const allPageConfigs = computed<PageConfigWithPath[]>(() => {
    return getAllPageConfigs()
  })

  // 获取菜单配置
  const menuConfigs = computed<PageConfigWithPath[]>(() => {
    return allPageConfigs.value.filter(config => config.showInMenu !== false)
  })

  // 根据分组获取配置
  const getConfigsByGroup = (group: string) => {
    return allPageConfigs.value.filter(config => config.group === group)
  }

  // 根据路径获取配置
  const getConfigByPath = (path: string) => {
    return getPageConfigByPath(path)
  }

  return {
    currentPageConfig,
    allPageConfigs,
    menuConfigs,
    getConfigsByGroup,
    getConfigByPath
  }
}